package com.xyhy.core.base.model.dto;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.util.CollectionUtils;

import com.xyhy.core.base.entity.ITreeNode;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@ApiModel(value = "树节点DTO基类")
public class TreeNodeDTO<ID> extends ObjectDTO<ID> implements ITreeNode<ID> {
  @ApiModelProperty(value = "父节点")
  private ID parentId;

  @ApiModelProperty(value = "节点路径的全路径，用'.'分割")
  private String location;

  @ApiModelProperty(value = "节点类型")
  private String clz;

  @ApiModelProperty(value = "节点序号")
  private Integer idx;

  @ApiModelProperty(value = "是否禁用该节点")
  private Boolean disabled;

  @ApiModelProperty(value = "扩展属性，子节点列表")
  private List<Object> children;

  @ApiModelProperty(value = "界面属性，是否叶节点")
  private Boolean isLeaf;

  @ApiModelProperty(value = "界面属性，节点图标")
  private String icon;

  @ApiModelProperty(value = "界面属性，是否展开")
  private boolean expand;

  public void addChild(Object node) {
    if (children == null) {
      children = new ArrayList<>();
    }
    children.add(node);
  }

  public boolean getIsLeaf() {
    return Optional.ofNullable(isLeaf)
            .orElseGet(() -> Optional.ofNullable(children).map(x -> CollectionUtils.isEmpty(x)).orElse(false));
  }

  public String getLabel() {
    return this.getName();
  }
}
